﻿using System;
using System.Collections.Generic;
using Model;


namespace BLL
{
    public class Product
    {
        //展示商品信息列表(返回所有的商品信息除图片)
        public List<ProductInfo> ShowProductList()
        {
            DAL.Product dal = new DAL.Product();
            return dal.GetProduct();
        }
        //展示商品信息(返回所有的商品信息除图片)
        public ProductInfo ShowProductInfo(string Pid)
        {
            DAL.Product dal = new DAL.Product();
            return dal.GetProductById(Pid);
        }
        //根据tag展示商品信息列表
        public List<ProductInfo> ShowProListByTag(string tag)
        {
            DAL.Product dal = new DAL.Product();
            return dal.GetProuctByTag(tag);
        }
        //根据商品id删除商品信息
        public int DeleteProduct(string Pid)
        {
            DAL.Product dal = new DAL.Product();
            return dal.DeleteProduct(Pid);
        }
        //根据商品id修改商品信息
        public int UpdateProduct(string Pid, ProductInfo proinfo)
        {
            DAL.Product dal = new DAL.Product();
            return dal.UpdateProduct(Pid, proinfo);
        }
        //根据商品id添加商品信息
        public int AddProduct(ProductInfo proinfo)
        {
            DAL.Product dal = new DAL.Product();
            return dal.InsertProduct(proinfo);
        }
        //判断数据库中是否有该商品id的图片
        public bool CheckProductImg(string Pid)
        {
            DAL.Product dal = new DAL.Product();
            if (dal.GetProductImg(Pid) != null) return true;
            return false;
        }
        //展示商品图片
        public byte[] ShowProductImg(string Pid)
        {
            DAL.Product dal = new DAL.Product();
            return dal.GetProductImg(Pid);
        }
        //向MongoDB数据库中添加图片
        public int AddProductImg(string Pid, byte[] newimg)
        {
            DAL.Product dal = new DAL.Product();
            return dal.InsertProductImg(Pid, newimg);
        }
        //向MongoDB数据库中更改图片
        public int UpdateProductImg(string Pid, byte[] newimg)
        {
            DAL.Product dal = new DAL.Product();
            return dal.UpdateProductImg(Pid, newimg);
        }
        //向MongoDB数据库中删除图片
        public int DeleteProductImg(string Pid)
        {
            DAL.Product dal = new DAL.Product();
            return dal.DeleteProductImg(Pid);
        }
        //根据价格对商品信息进行排序(升序或降序)(method表示升序或降序 升序为asc，降序为desc)
        public List<ProductInfo> ProductOrder(string method, List<ProductInfo> list)
        {
            if (method == "asc")
            {
                list.Sort((ProductInfo x, ProductInfo y) => {
                    if (x.Pprice < y.Pprice) return 1;
                    else if (x.Pprice > y.Pprice) return -1;
                    else return 0;
                });
                return list;
            }
            else
            {
                list.Sort((ProductInfo x, ProductInfo y) => {
                    if (x.Pprice > y.Pprice) return 1;
                    else if (x.Pprice < y.Pprice) return -1;
                    else return 0;
                });
                return list;
            }
        }
        //商品推荐
        public List<ProductInfo> RcmdProducts()
        {
            DAL.Product dal = new DAL.Product();
            return dal.GetRcmdProducts();
        }
        //新品推荐
        public List<ProductInfo> NewProducts(DateTime today)
        {
            DAL.Product dal = new DAL.Product();
            return dal.GetNewProducts(today);
        }
        //商品搜索
        public List<ProductInfo> SearchProduct(string name)
        {
            DAL.Product dal = new DAL.Product();
            return dal.GetProductsByname(name);
        }
        //判断商品是否缺货
        public bool CheckProductNum(string Pid)
        {
            DAL.Product dal = new DAL.Product();
            ProductInfo pro = dal.GetProductById(Pid);
            if (pro.Pnum == 0) return false;
            else return true;
        }
    }
}
